package kayao.client;

import kayao.client.data.SharedMemory;
import kayao.client.input.AccelerometerInput;
import kayao.client.input.KeyInput;
import kayao.client.network.NetreadThread;
import kayao.client.network.NetwriteThread;
import kayao.client.network.message.MessageHandler;
import kayao.client.scene.AreaManager;
import kayao.client.scene.KayaoView;
import kayao.client.scene.ModellingThread;

import android.app.Activity;
import android.os.Bundle;
import android.os.PowerManager;

/**
* An Activity class working as the starting point for the Kayao game.
*/
public class KayaoActivity extends Activity {
	private KayaoView mKayaoView;
	private AccelerometerInput accelerometerInput;
	private KeyInput buttonInput;
	private NetreadThread mNetreadThread;
	private NetwriteThread mNetwriteThread;
	private ModellingThread mModellingThread;
	
	private PowerManager.WakeLock mWakeLock;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Create display subsystem and tell the activity to draw it on the screen
		mKayaoView = new KayaoView(this);
		setContentView(mKayaoView);
		
		final PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
        this.mWakeLock.acquire();
	}
	
	/** Called when the activity is ready to initialize. */
	public void onInitialize() {		
		SharedMemory.getInstance().createWorld(this);
		mKayaoView.setScene(SharedMemory.getInstance().getScene());
		mKayaoView.setCamera(SharedMemory.getInstance().getCamera());
		
		// Create system threads
		mModellingThread = new ModellingThread();
		mKayaoView.setEvent(mModellingThread);
		
		accelerometerInput = new AccelerometerInput(this);
		buttonInput = new KeyInput();
		
		mNetreadThread = new NetreadThread();
		mNetwriteThread = new NetwriteThread(mKayaoView);
		MessageHandler.initialize(mNetwriteThread, mNetreadThread);
		AreaManager.initialize();
	}

	/** Called when all initialization is complete. */
	public void onInitializationComplete() {
		// Running garbage collector after initializing memory allocations
		Runtime r = Runtime.getRuntime();
		r.gc();
		
		// Start running system threads
		mKayaoView.setOnKeyListener(buttonInput);
		mKayaoView.setFocusable(true);
		mKayaoView.setFocusableInTouchMode(true);
		
		MessageHandler.start();
		mNetreadThread.start();
		mNetwriteThread.start();	
		
		accelerometerInput.start();
	}
	
	public void onLogOut() {
		MessageHandler.printStats();
		while(mModellingThread.terminate()){}
		mModellingThread = null;
		System.out.println("ModellingThread closed");
		mKayaoView = null;
		accelerometerInput.terminate();
		accelerometerInput = null;
		buttonInput = null;
		System.out.println("Set accelerometer and buttoninput to null");
		while(mNetwriteThread.terminate()){}
		mNetwriteThread = null;
		System.out.println("Netwrite closed");
		while(mNetreadThread.terminate()){}
		mNetreadThread = null;
		System.out.println("Netread closed");
		AreaManager.terminate();
		while(MessageHandler.terminate()){}
		System.out.println("MessageHandler closed");
		SharedMemory.getInstance().reset();
		// Garbage collect
		Runtime r = Runtime.getRuntime();
		r.gc();
		System.out.println("### KayaoActivity.onLogOut finished");
	}
}
